package com.life.dao.service;
import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.jdbc.Work;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.life.util.CommUtil;
import com.life.util.DateUtil;
import com.life.util.StringUtil;

public class GenericDaoHibernate<T, PK extends Serializable> extends HibernateDaoSupport implements IGenericDao<T, PK> {

	protected final Log log = LogFactory.getLog(getClass());
	private final Class<T> persistentClass;

	public GenericDaoHibernate(final Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	public void clearFlush() {
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
	}

	public void updateObj(Object obj) {
		super.getHibernateTemplate().update(obj);
	}

	public Object exceuteSql(String sql){
		final String finalSql = sql;
		Object result = (Object) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Integer result = session.createSQLQuery(finalSql).executeUpdate();
				return result;
			}
		});
		return result;
	}
	
	public void update(T object) {
		super.getHibernateTemplate().update(object);
	}

	public void evict(Object obj) {
		super.getHibernateTemplate().evict(obj);
	}

	public void updateList(List<T> list) {
		for (T obj : list)
			this.update(obj);
	}

	public void saveObj(Object obj) {
		super.getHibernateTemplate().save(obj);
	}

	public void saveOrUpdateObj(Object obj) {
		super.getHibernateTemplate().saveOrUpdate(obj);
	}

	public List<T> getAll() {
		return super.getHibernateTemplate().loadAll(this.persistentClass);
	}

	public List<T> getAllDistinct() {
		Collection result = new LinkedHashSet(getAll());
		return new ArrayList(result);
	}

	public T get(PK id) {
		T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);

		/*
		 * if (entity == null) { log.warn("Uh oh, '" + this.persistentClass + "'
		 * object with id '" + id + "' not found..."); throw new
		 * ObjectRetrievalFailureException(this.persistentClass, id); }
		 */

		return entity;
	}

	public List<T> getDocAll(Map<String, Object> parmMap) {
		DetachedCriteria Criteria = DetachedCriteria.forClass(persistentClass);
		for (Iterator<String> iter = parmMap.keySet().iterator(); iter.hasNext();) {
			String key = iter.next();
			Criteria.add(Restrictions.eq(key, parmMap.get(key)));
		}
		return (List<T>) super.getHibernateTemplate().findByCriteria(Criteria);
	}

	public List<T> getAll(Map<String, Object> equalMap, Map<String, Object> orderMap) {
		DetachedCriteria Criteria = DetachedCriteria.forClass(persistentClass);
		if (equalMap != null && !equalMap.isEmpty())
			for (Iterator<String> iter = equalMap.keySet().iterator(); iter.hasNext();) {
				String key = iter.next();
				Criteria.add(Restrictions.eq(key, equalMap.get(key)));
			}
		if (orderMap != null && !orderMap.isEmpty()) {
			for (Iterator<String> iter = orderMap.keySet().iterator(); iter.hasNext();) {
				String key = iter.next();
				String value = (String) orderMap.get(key);
				if (value.equals("asc")) {
					Criteria.addOrder(Order.asc(key));
				}
				if (value.equals("desc")) {
					Criteria.addOrder(Order.desc(key));
				}
			}
		}
		return (List<T>) super.getHibernateTemplate().findByCriteria(Criteria);
	}

	public boolean exists(PK id) {
		T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);
		return entity != null;
	}

	public T save(T object) {
		// T obj = (T) super.getHibernateTemplate().merge(object);
		T obj = (T) super.getHibernateTemplate().save(object);
		return obj;
	}

	public void saveList(List<T> list) {
		super.getHibernateTemplate().saveOrUpdate(list);
	}

	public void mergeObj(Object entity) {
		super.getHibernateTemplate().merge(entity);
	}

	public void saveListObj(List list) {
		super.getHibernateTemplate().saveOrUpdate(list);
	}

	public void remove(PK id) {
		super.getHibernateTemplate().delete(this.get(id));
	}

	public void remove(T object) {
		super.getHibernateTemplate().delete(object);
	}
	
	public void del(Object entity) {
		super.getHibernateTemplate().delete(entity);
	}

	public void delAll(List entities) {
		super.getHibernateTemplate().deleteAll(entities);
	}

	public void flush() {
		super.getHibernateTemplate().flush();
	}

	public List<T> findByNamedQuery(String queryName, Map<String, Object> nameParas) {
		return (List<T>) getListByNamedQueryParas(queryName, nameParas);
	}

	public List<Object> getListByNamedQueryParas(final String queryName, final Map<String, Object> nameParas) {
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryName);
				String[] namedParam = query.getNamedParameters();
				if (nameParas != null && nameParas.size() != 0) {
					for (Iterator<String> iter = nameParas.keySet().iterator(); iter.hasNext();) {
						String key = iter.next();
						if (ArrayUtils.contains(namedParam, key)) {
							Object obj = nameParas.get(key);
							if (obj == null) {
								query.setParameter(key, null);
							} else if (obj.getClass().isArray()) {
								Object[] objArr = (Object[]) obj;
								if (objArr.length > 0)
									query.setParameterList(key, objArr);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Collection) {
								Collection objColl = (Collection) obj;
								if (objColl.size() > 0)
									query.setParameterList(key, objColl);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Date) {
								query.setTimestamp(key, (Date) obj);
							} else {
								query.setParameter(key, obj);
							}
						}
					}
				}
				return query.list();
			}
		});
		return list;
	}

	public List getListByNamedQuery(final String queryName) {
		return (List) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				return session.getNamedQuery(queryName).list();
			}
		});
	}

	public int excuteUpdate(String queryNameStr, Map<String, Object> parmMap) {
		final Map<String, Object> queryMap = parmMap;
		final String queryName = queryNameStr;
		// getHibernateTemplate().setFlushMode(3);
		// getHibernateTemplate().flush();
		// getHibernateTemplate().clear();
		return (Integer) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Integer doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryName);
				String[] namedParam = query.getNamedParameters();
				if (queryMap != null && queryMap.size() != 0) {
					for (Iterator<String> iter = queryMap.keySet().iterator(); iter.hasNext();) {
						String key = iter.next();
						if (ArrayUtils.contains(namedParam, key)) {
							Object obj = queryMap.get(key);
							if (obj == null) {
								query.setParameter(key, null);
							} else if (obj.getClass().isArray()) {
								Object[] objArr = (Object[]) obj;
								if (objArr.length > 0)
									query.setParameterList(key, objArr);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Collection) {
								Collection objColl = (Collection) obj;
								if (objColl.size() > 0)
									query.setParameterList(key, objColl);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Date) {
								query.setTimestamp(key, (Date) obj);
							} else {
								query.setParameter(key, obj);
							}
						}
					}
				}
				return query.executeUpdate();
			}
		});

	}

	public int excuteUpdateColl(String queryNameStr, Object[] obj) {
		final Object[] objr = obj;
		final String queryName = queryNameStr;
		// getHibernateTemplate().setFlushMode(3);
		// getHibernateTemplate().flush();
		// getHibernateTemplate().clear();
		return (Integer) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Integer doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryName);
				if (objr != null && objr.length != 0) {
					int length = objr.length;
					for (int i = 0; i < length; i++) {
						Object obj = objr[i];
						if (obj instanceof Date)
							query.setTimestamp(i, (Date) obj);
						else
							query.setParameter(i, obj);
					}
				}
				return query.executeUpdate();
			}
		});

	}


	public List<Object> getListBy(Class<T> clazz) {
		return (List<Object>) getHibernateTemplate().find(clazz.getSimpleName());
	}

	public List<Object> getListByParm(String queryName, Object[] value) {
		return getListByParm(queryName, value, null);
	}

	public List<Object> getListByParm(String queryName, String[] parms, Object[] value) {
		return getListByParm(queryName, parms, value);
	}
	public List<Object> getListByParm(String queryName, Object[] value, Class cls, int start, int pagesize) {
		final String queryNamef = queryName;
		final Object[] valuef = value;
		final Class clsf = cls;
		final int startf = start;
		final int pagesizef = pagesize;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryNamef).setResultTransformer(new BeanTransformer(clsf));
				query.setFirstResult(startf);
				query.setMaxResults(pagesizef);
				if (valuef != null && valuef.length > 0) {
					int length = valuef.length;
					for (int i = 0; i < length; i++) {
						Object obj = valuef[i];
						if (obj instanceof Date)
							query.setTimestamp(i, (Date) obj);
						else
							query.setParameter(i, obj);
					}
				}
				return query.list();
			}
		});
		return list;
	}

	public List<Object> getListByParm(String queryName, Object[] value, Class cls) {
		final String queryNamef = queryName;
		final Object[] valuef = value;
		final Class clsf = cls;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryNamef).setResultTransformer(new BeanTransformer(clsf));
				if (valuef != null && valuef.length > 0) {
					int length = valuef.length;
					for (int i = 0; i < length; i++) {
						Object obj = valuef[i];
						if (obj instanceof Date)
							query.setTimestamp(i, (Date) obj);
						else
							query.setParameter(i, obj);
					}
				}
				return query.list();
			}
		});
		return list;
	}

	public List<Object> getListByParm(String queryName, String[] parms, Object[] value, Class cls) {
		final String queryNamef = queryName;
		final String[] parmsf = parms;
		final Object[] valuef = value;
		final Class clsf = cls;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryNamef).setResultTransformer(new BeanTransformer(clsf));
				String[] namedParam = query.getNamedParameters();
				if (parmsf != null && parmsf.length > 0) {
					int length = parmsf.length;
					for (int i = 0; i < length; i++) {
						String key = parmsf[i];
						if (ArrayUtils.contains(namedParam, key)) {
							Object obj = valuef[i];
							if (obj == null) {
								query.setParameter(key, null);
							} else if (obj.getClass().isArray()) {
								Object[] objArr = (Object[]) obj;
								if (objArr.length > 0)
									query.setParameterList(key, objArr);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Collection) {
								Collection objColl = (Collection) obj;
								if (objColl.size() > 0)
									query.setParameterList(key, objColl);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Date) {
								query.setTimestamp(key, (Date) obj);
							} else {
								query.setParameter(key, obj);
							}
						}
					}
				}
				return query.list();
			}
		});
		return list;
	}

	public List<Object> getListByParm(String queryName, Map<String, Object> pram, Class cls) {
		final String queryNamef = queryName;
		final Map<String, Object> pramf = pram;
		final Class clsf = cls;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryNamef).setResultTransformer(new BeanTransformer(clsf));
				String[] namedParam = query.getNamedParameters();
				if (pramf != null && pramf.size() != 0) {
					for (Iterator<String> iter = pramf.keySet().iterator(); iter.hasNext();) {
						String key = iter.next();
						if (ArrayUtils.contains(namedParam, key)) {
							Object obj = pramf.get(key);
							if (obj == null) {
								query.setParameter(key, null);
							} else if (obj.getClass().isArray()) {
								Object[] objArr = (Object[]) obj;
								if (objArr.length > 0)
									query.setParameterList(key, objArr);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Collection) {
								Collection objColl = (Collection) obj;
								if (objColl.size() > 0)
									query.setParameterList(key, objColl);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Date) {
								query.setTimestamp(key, (Date) obj);
							} else {
								query.setParameter(key, obj);
							}
						}
					}
				}
				return query.list();
			}
		});
		return list;
	}

	public List<Object> getPageListByParm(String queryName, Map<String, Object> pram, Class cls, int start, int pagesize) {
		final String queryNamef = queryName;
		final Map<String, Object> pramf = pram;
		final Class clsf = cls;
		final int startf = start;
		final int pagesizef = pagesize;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryNamef).setResultTransformer(new BeanTransformer(clsf));
				String[] namedParam = query.getNamedParameters();
				query.setFirstResult(startf);
				query.setMaxResults(pagesizef);
				if (pramf != null && pramf.size() != 0) {
					for (Iterator<String> iter = pramf.keySet().iterator(); iter.hasNext();) {
						String key = iter.next();
						if (ArrayUtils.contains(namedParam, key)) {
							Object obj = pramf.get(key);
							if (obj == null) {
								query.setParameter(key, null);
							} else if (obj.getClass().isArray()) {
								Object[] objArr = (Object[]) obj;
								if (objArr.length > 0)
									query.setParameterList(key, objArr);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Collection) {
								Collection objColl = (Collection) obj;
								if (objColl.size() > 0)
									query.setParameterList(key, objColl);
								else
									query.setParameter(key, "");
							} else if (obj instanceof Date) {
								query.setTimestamp(key, (Date) obj);
							} else {
								query.setParameter(key, obj);
							}
						}
					}
				}
				return query.list();
			}
		});
		return list;
	}

	public List<Object> getListBySql(String queryName, String whereSql, Class cls, int start, int pagesize) {
		final Class clsf = cls;
		final String whereSqls = whereSql;
		final int starts = start;
		final int pagesizes = pagesize;
		final String queryNames = queryName;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery((session.getNamedQuery(queryNames).getQueryString()) + " " + whereSqls)
						.setResultTransformer(new BeanTransformer(clsf));
				query.setFirstResult(starts);
				query.setMaxResults(pagesizes);
				List listtt = query.list();
				return listtt;
			}
		});
		return list;
	}
	
	public List<Object> getListByNativeSql(String sql, Class cls, int start, int pagesize) {
		final Class clsf = cls;
		final String sqls = sql;
		final int starts = start;
		final int pagesizes = pagesize;
	
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sqls)
						.setResultTransformer(new BeanTransformer(clsf));
				query.setFirstResult(starts);
				query.setMaxResults(pagesizes);
				List listtt = query.list();
				return listtt;
			}
		});
		return list;
	}
	
	public List<Object> getListByNativeSql(String sql, int start, int pagesize) {
		final String sqls = sql;
		final int starts = start;
		final int pagesizes = pagesize;
	
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sqls);
				query.setFirstResult(starts);
				query.setMaxResults(pagesizes);
				List listtt = query.list();
				return listtt;
			}
		});
		return list;
	}
	
	public List<Object> getListByNativeSql(String sql) {
	
		final String sqls = sql;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sqls);
				List listtt = query.list();
				return listtt;
			}
		});
		return list;
	}

	public List<Object> getListBySql(String queryName, String whereSql, Class cls) {

		final Class clsf = cls;
		final String whereSqls = whereSql;
		final String queryNames = queryName;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String sql = session.getNamedQuery(queryNames).getQueryString() + " " + whereSqls;
				Query query = session.createSQLQuery(sql).setResultTransformer(new BeanTransformer(clsf));
				List listtt = query.list();
				return listtt;
			}
		});
		return list;
	}


	public List<Object> getListByCriteria(DetachedCriteria criteria) {
		return (List<Object>) this.getHibernateTemplate().findByCriteria(criteria);
	}
	
	public List<Object> getListByCriteria(DetachedCriteria criteria,int start,int maxResults) {
		return (List<Object>) this.getHibernateTemplate().findByCriteria(criteria, start, maxResults);
	}

	public ClassMetadata getClassMetadata(Class<?> clsPo) {
		return getHibernateTemplate().getSessionFactory().getClassMetadata(clsPo);
	}

	public AbstractEntityPersister getEntityPersister(Class<?> clsPo) {
		return (SingleTableEntityPersister) getHibernateTemplate().getSessionFactory().getClassMetadata(clsPo);
	}
	
	/**###########################   新增      ###########################*/
	public void updateBySql(final String sql){
		getHibernateTemplate().executeWithNativeSession(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				return query.executeUpdate();
			}
		});
	}

	@Override
	public Object saveObjSuper(Object obj) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().save(obj);
	}
	
	
	
	  @SuppressWarnings({"unchecked"})
	    public Integer getCountByNativeSql(String sql)
	    {
	        final String sqls = sql;
	        @SuppressWarnings("rawtypes")
	        Integer count = Integer.parseInt(getHibernateTemplate().executeWithNativeSession(new HibernateCallback()
	        {
	            public Object doInHibernate(Session session)
	                throws HibernateException, SQLException
	            {
	                return session.createSQLQuery(sqls).uniqueResult();
	            }
	        }).toString());
	        return count;
	    }

	@Override
	public List execProc(List inputArgs1,List outTypes1,String procedureName1)
			throws Exception {
		final List inputArgs = inputArgs1;
		final List outTypes = outTypes1;
		final String procedureName = procedureName1;
		//如果输入参数集合 活 输出参数类型集合中有NULL 返回NULL
		if(StringUtil.isEmpty(inputArgs) || StringUtil.isEmpty(outTypes1)){
			List returnList = new ArrayList();
			int length = outTypes.size();
			for (int j = 0; j < length; j++) {
				/*if(j==(length-1))
				{
					returnList.add("N|传入参数有NULL值!");
				}else{*/
					returnList.add(j);
				//}
			}
			return returnList;
		}
		List list = getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                    	final List returnList = new ArrayList();
                    	session.doWork(new Work(){
							@Override
							public void execute(Connection conn)
									throws SQLException {
								
								
								//1.argsStr 定义占位符
		                		String argsStr = "";
		                		int outputNum = outTypes.size();
		                		if(!StringUtil.isEmpty(inputArgs)){
		                			argsStr = CommUtil.makeArgs(inputArgs.size()+outputNum);
		                		}
		                    	
		                    	//2.获取session 执行调用过程
		                        CallableStatement call = conn.prepareCall("{call "+procedureName+"("+argsStr+")}");
		                		
		                		//3.设置输入Statement
		                		int postion = 1;
		                		for (int i = 0; i < inputArgs.size(); i++) {
		                			if (inputArgs.get(i)  instanceof String) {
		                				call.setString(postion,inputArgs.get(i).toString());
		                				postion++;
		                				continue;
		                			}else if(inputArgs.get(i)  instanceof Integer){
		                				call.setInt(postion,Integer.parseInt(inputArgs.get(i).toString()));
		                				postion++;
		                				continue;
		                			}else if(inputArgs.get(i)  instanceof Double){
		                				call.setDouble(postion,Double.valueOf(inputArgs.get(i).toString()));
		                				postion++;
		                				continue;
		                			}else if(inputArgs.get(i)  instanceof Boolean){
		                				call.setBoolean(postion,Boolean.valueOf((inputArgs.get(i).toString())));
		                				postion++;
		                				continue;
		                			}else if(inputArgs.get(i)  instanceof Date){
		                				call.setDate(postion,new java.sql.Date( DateUtil.ConvertToDate(inputArgs.get(i)).getTime()));
		                				postion++;
		                				continue;
		                			}else if(inputArgs.get(i)  instanceof Float){
		                				call.setFloat(postion,Float.valueOf(inputArgs.get(i).toString()));
		                				postion++;
		                				continue;
		                			}
		                		}
		                		//4.设置输出Statement
		                		int outPostion = postion;
		                		for (int i = 0; i < outTypes.size(); i++) {
		                			if(outTypes.get(i).equals("S")){
		                				call.registerOutParameter(postion, Types.VARCHAR);
		                				postion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("I")){
		                				call.registerOutParameter(postion, Types.INTEGER);
		                				postion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("D")){
		                				call.registerOutParameter(postion, Types.DOUBLE);
		                				postion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("B")){
		                				call.registerOutParameter(postion, Types.BOOLEAN);
		                				postion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("F")){
		                				call.registerOutParameter(postion, Types.FLOAT);
		                				postion++;
		                				continue;
		                			}
		                		}
		                		//5.执行
		                		call.execute();
		                		//6.输出返回
		                		for (int i = 0; i < outTypes.size(); i++) {
		                			if(outTypes.get(i).equals("S")){
		                				returnList.add(call.getString(outPostion));
		                				outPostion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("I")){
		                				returnList.add(call.getInt(outPostion));
		                				outPostion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("D")){
		                				returnList.add(call.getDouble(outPostion));
		                				outPostion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("B")){
		                				returnList.add(call.getBoolean(outPostion));
		                				outPostion++;
		                				continue;
		                			}else if(outTypes.get(i).equals("F")){
		                				returnList.add(call.getFloat(outPostion));
		                				outPostion++;
		                				continue;
		                			}
		                		}
		                		
		                		call.clearParameters();
							}
                    	});
                		return returnList;
                    }
                });
		
		return list;
	}

	@Override
	public List<Object> getListByNativeSql(String sql, Class cls) {
		final Class clsf = cls;
		final String sqls = sql;
		List<Object> list = (List<Object>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sqls)
						.setResultTransformer(new BeanTransformer(clsf));
				List listtt = query.list();
				return listtt;
			}
		});
		return list;
	}
}

